package com.niji.littly_donkey_android.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import com.makeramen.roundedimageview.RoundedImageView;
import com.niji.littly_donkey_android.R;
import com.niji.littly_donkey_android.bean.Comment;
import com.niji.littly_donkey_android.bean.Photo;
import com.niji.littly_donkey_android.util.BitMapUtil;
import com.niji.littly_donkey_android.util.CirclePhotoUtil;
import com.niji.littly_donkey_android.util.NetworkGlideImageLoader;
import com.niji.littly_donkey_android.util.UrlUtil;
import com.niji.littly_donkey_android.view.CircleImageView;
import com.niji.littly_donkey_android.view.StarRatingView;
import org.w3c.dom.Text;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

public class CommentAdapter extends ArrayAdapter<Comment> {


    private static final String TAG = "CommentAdapter";

    private Context context;

    public CommentAdapter(Context context, ArrayList<Comment> members)
    {
        super(context,0, members);
        this.context = context;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Comment comment = getItem(position);

        if(convertView == null)
        {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_comment, parent, false);
        }

        CircleImageView roundedImageView = convertView.findViewById(R.id.comment_head);
        RecyclerView commentPhotos = convertView.findViewById(R.id.comment_photos);
        TextView commentTitle = convertView.findViewById(R.id.comment_title);
        TextView commentId = convertView.findViewById(R.id.comment_id);
        TextView commentTime = convertView.findViewById(R.id.comment_time);
        TextView commentMessage = convertView.findViewById(R.id.comment_message);
        StarRatingView commentStar = convertView.findViewById(R.id.comment_star);
        //thumb.setImageBitmap(BitMap.getInstance().returnBitMap(u.getThumb()));
        NetworkGlideImageLoader.getInstance().displayImage((Activity) context,comment.getHead(),roundedImageView,0,0);
        ArrayList<Photo> photos = new ArrayList<>();
        List<String> photos1 = comment.getPhotos();
        for (int i = 0; i < photos1.size(); i++) {
            Photo photo = new Photo();
            photo.setUrl(UrlUtil.URL + photos1.get(i));
            photos.add(photo);
        }
        commentPhotos.setHasFixedSize(true);
        commentPhotos.setLayoutManager(new StaggeredGridLayoutManager(4,StaggeredGridLayoutManager.VERTICAL));
        commentPhotos.setAdapter(new CommentPhotosAdapter(context,photos));
        commentId.setText("" + comment.getId());
        commentMessage.setText(comment.getMessage());
        commentStar.setRate(comment.getRate());
        commentTime.setText(comment.getTime());
        commentTitle.setText(comment.getTitle());
        return convertView;
    }

}



